package com.xyz.musicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import beans.User;

public class UserDetailActivity extends AppCompatActivity {

    //声明控件
    TextView tvForUserName;
    ListView lvForOption;
    Intent intent;
    User userAtPresent;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_detail);

        init();

        //操作栏添加导航按钮
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch ( item.getItemId() ){
            case android.R.id.home:{
                finish();
            }break;
            default:
                Toast.makeText(this, "返回", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

    private void init(){
        //实例化控件
        tvForUserName = (TextView) findViewById(R.id.tv_10);
        lvForOption = (ListView) findViewById(R.id.lv_userdetail);

        //获取来自主界面的intent
        intent = getIntent();
        userAtPresent = (User) intent.getSerializableExtra("user");

        //设置控件的值
        tvForUserName.setText(userAtPresent.getUsername());

        //对listview进行操作
        List<String> list = new ArrayList<>(Arrays.asList("历史记录","充值VIP","小游戏","生成歌单"));

        adapter = new ArrayAdapter<>(UserDetailActivity.this, android.R.layout.simple_list_item_1,list);

        lvForOption.setAdapter(adapter);


        //listview事件监听
        lvForOption.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch ( lvForOption.getItemAtPosition(position).toString() ){
                    case "历史记录":{
                        Intent tempIntent = new Intent(UserDetailActivity.this,ShowHistoryActivity.class);
                        tempIntent.putExtra("user",userAtPresent);
                        startActivity(tempIntent);
                    }break;
                    case "充值VIP":{

                    }break;
                    default:
                        Toast.makeText(UserDetailActivity.this,"error",Toast.LENGTH_SHORT);
                        break;
                }
            }
        });

    }

}